﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

using ASBCC.Database.Entity;
using ASBCC.Database.Service;

namespace BEGDC.CC.Web.Workflow.Dialog
{
    public partial class AskForMore : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (ProcInstId <= 0) return;

                hidCommentRequired.Value = Resources.Workflow.CommentRequired;
                hidAskForMoreTargetRequired.Value = Resources.Workflow.AskForMoreTargetRequired;
                hidProcInstId.Value = ProcInstId.ToString();
                DataTable dt = GetDataSource();
                lstSelectUsers.DataSource = dt;
                lstSelectUsers.DataTextField = "DataTextField";
                lstSelectUsers.DataValueField = "DataValueField";
                lstSelectUsers.DataBind();
                //lstSelectUsers.SelectedIndex = 0;
                if (dt.Rows.Count == 1)
                    lstSelectUsers.SelectedValue = dt.Rows[0]["DataValueField"].ToString();
            }
        }

        private DataTable GetDataSource()
        {
            DataSet ds = DatabaseManager.usp3_Select_AskForMoreUserList(false, ProcInstId);
            if (ds == null || ds.Tables.Count == 0) return null;
            DataTable dt = ds.Tables[0];
            if (dt.Rows.Count == 0) return null;
            DataTable dtProcessed = dt.Clone();
            foreach (DataRow dr in dt.Rows)
            {
                string activityName = dr["ActivityName"] == DBNull.Value ? string.Empty : dr["ActivityName"].ToString();
                string displayOwnerEn = dr["DisplayOwnerEn"] == DBNull.Value ? string.Empty : dr["DisplayOwnerEn"].ToString();
                string displayOwnerCn = dr["DisplayOwnerCn"] == DBNull.Value ? string.Empty : dr["DisplayOwnerCn"].ToString();
                DataRow[] rows = dtProcessed.Select(string.Format("ActivityName='{0}' and DisplayOwnerEn='{1}' and DisplayOwnerCn='{2}' ", activityName, displayOwnerEn, displayOwnerCn));
                if (rows == null || rows.Length == 0)
                {
                    dtProcessed.Rows.Add(dr.ItemArray);
                }
            }
            dtProcessed.AcceptChanges();
            return dtProcessed;
        }

        private int ProcInstId
        {
            get
            {
                int procInstId;
                bool res = int.TryParse(Request.QueryString["ProcInstID"], out procInstId);
                if (res) return procInstId;
                return -1;
            }
        }
    }
}
